/*
by txdnet.cn tonydon
 */

package txdnet.util.image;

import java.awt.Font;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import txdnet.util.DataUtil;

/**
 * @see
<pre>
扩展验证码servlet
public class MyAuthCodeServlet extends AuthCodeBaseServlet{
    private  int fontsize = 21;
    private  Font[] font ={ new Font("宋体", Font.PLAIN, fontsize),new Font("Tahoma", Font.PLAIN, fontsize) };
    
    public MyAuthCodeServlet(){
        super.setBrot(false);
        super.setFontsize(fontsize);
        super.setFont(font);
    }
}
</pre>
 * @author txdnet
 */
public class AuthCodeBaseServlet extends HttpServlet {
    private static final long serialVersionUID = -3991275181268019609L;

   private  int[] fontcolor = {0x555555,0xEC186A,0x3EA874};
   private  int[] bgcolor = {0xF5F5F5};
   private  char[] mapTable = {
        '1', '2', '3', '4', '5', '6', '7', '8', '9', 
        'a','b', 'c', 'd', 'e', 'g', 'h', 'k','m','n',
        'p','q','r','s','t','u','v','w','x','y','z',
        'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
        'P','Q','R','S','T','U','V','X','Y','Z'
    };
   private String sessionName = "vcode";
   private int charNum = 4 ;
   private int imgW = 95;
   private int imgH = 45;
   private int fontSize = 30;
   private int pixNum = 64;
   private boolean bMixcolr = true;
   private boolean bRot = true;
   private boolean bDwLine = false;
   private boolean bDwArc = true;
   private  Font[] font ={ 
       new Font("Tahoma", Font.PLAIN, fontSize-3) 
   };

    public void setFontcolor(int[] fontcolor){
        this.fontcolor = fontcolor;
    }
    
    public void setBgcolor(int[] bgcolor){
        this.bgcolor = bgcolor;
    }
    
    
    public void setMaptable(char[] mapTable){
        this.mapTable = mapTable;
    }
    
    public void setSessionname(String sessionName){
        this.sessionName = sessionName;
    }
    
    public void setCharnum(int charNum){
        this.charNum = charNum;
    }
    
    public void setImgw(int imgW){
        this.imgW = imgW;
    }
    
    public void setImgh(int imgH){
        this.imgH = imgH;
    }
    
    public void setFontsize(int fontSize){
        this.fontSize = fontSize;
    }
    
    public void setPixnum(int pixNum){
        this.pixNum = pixNum;
    }
    
    public void setBmixcolr(boolean bMixcolr){
        this.bMixcolr = bMixcolr;
    }
    
    public void setBrot(boolean bRot){
        this.bRot = bRot;
    }
    
    public void setBdwLine(boolean bDwLine){
        this.bDwLine = bDwLine;
    }
    
    public void setBdwArc(boolean bDwArc){
        this.bDwArc = bDwArc;
    }
    
    public AuthCodeBaseServlet(){
    }

       @Override
    public void init(ServletConfig config) throws ServletException{
     super.init(config);
     }

    @Override
    public void destroy(){
    super.destroy();
    } 
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
public void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
       response.reset();
        response.setContentType( "image/png" );
        response.setHeader( "Pragma", "No-cache" );
        response.setHeader( "Cache-Control", "no-cache" );
        response.setDateHeader( "Expires", 0 );
txdnet.util.Context.setSessionAttribute(request, sessionName, null);
 
txdnet.util.Context.setSessionAttribute(request, sessionName, ImageVerifier.getImageStr(
        charNum, imgW, imgH, fontSize,
        pixNum, // 干扰点数量
        bMixcolr, // 是否使用混杂干扰点
        bRot, // 是否使用偏转字体
        fontcolor,
        bgcolor,
        bDwLine, // 是否使用直线干扰
        bDwArc, // 是否使用曲线干扰
        mapTable,
        font[DataUtil.getRndInt(0, font.length)],
        "png",
        response.getOutputStream()) );
 

/*
ImageVerifier.outputImage(
        imgW, imgH, fontSize,
        pixNum,
        bMixcolr,bRot,
        fontcolor,
        bgcolor,
        bDwLine,
        bDwArc,
        "8976",
        font[DataUtil.getRndInt(0, font.length)],
        "png",
        response.getOutputStream());
 * *
 */
} 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //    processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "verify image servlet";
    }// </editor-fold>

    /**
     * @return the font
     */
    public Font[] getFont() {
        return font;
    }

    /**
     * @param font the font to set
     */
    public void setFont(Font[] font) {
        this.font = font;
    }

}